package com.ruoyi.system.service.impl;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.ITCkOrderdetailService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 出库物料明细
 * Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-05
 */
@Service
public class TCkOrderdetailServiceImpl extends ServiceImpl<TCkOrderdetailMapper, TCkOrderdetail> implements ITCkOrderdetailService {

    @Autowired
    private TCkOrderdetailMapper tCkOrderdetailMapper;

    @Autowired
    private TMiStockMapper tMiStockMapper;

    @Autowired
    private TBaseStorageAreaLocationMapper locationMapper;

    @Autowired
    private TOngoodsshelfMapper ongoodsshelfMapper;

    @Autowired
    private TCkPickingwavegoodsMapper tCkPickingwavegoodsMapper;

    private static Random random = new Random();

    /**
     * 查询出库物料明细
     *
     * @param orderId 出库物料明细
     *                主键
     * @return 出库物料明细
     */
    @Override
    public TCkOrderdetail selectTCkOrderdetailByOrderId(String orderId) {
        return tCkOrderdetailMapper.selectTCkOrderdetailByOrderId(orderId);
    }

    /**
     * 查询出库物料明细
     * 列表
     *
     * @param tCkOrderdetail 出库物料明细
     * @return 出库物料明细
     */
    @Override
    public List<TCkOrderdetail> selectTCkOrderdetailList(TCkOrderdetail tCkOrderdetail) {
        return tCkOrderdetailMapper.selectTCkOrderdetailList(tCkOrderdetail);
    }

    /**
     * 新增出库物料明细
     *
     * @param tCkOrderdetail 出库物料明细
     * @return 结果
     */
    @Override
    public int insertTCkOrderdetail(TCkOrderdetail tCkOrderdetail) {
        return tCkOrderdetailMapper.insertTCkOrderdetail(tCkOrderdetail);
    }

    /**
     * 修改出库物料明细
     *
     * @param tCkOrderdetail 出库物料明细
     * @return 结果
     */
    @Override
    public int updateTCkOrderdetail(TCkOrderdetail tCkOrderdetail) {
        return tCkOrderdetailMapper.updateTCkOrderdetail(tCkOrderdetail);
    }

    /**
     * 批量删除出库物料明细
     *
     * @param orderIds 需要删除的出库物料明细
     *                 主键
     * @return 结果
     */
    @Override
    public int deleteTCkOrderdetailByOrderIds(String[] orderIds) {
        return tCkOrderdetailMapper.deleteTCkOrderdetailByOrderIds(orderIds);
    }

    /**
     * 删除出库物料明细
     * 信息
     *
     * @param orderId 出库物料明细
     *                主键
     * @return 结果
     */
    @Override
    public int deleteTCkOrderdetailByOrderId(String orderId) {
        return tCkOrderdetailMapper.deleteTCkOrderdetailByOrderId(orderId);
    }

    /**
     * 创建出库明细
     *
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int creates(OrderDetailCreateRequest request) {

        LambdaQueryWrapper<TMiStock> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(TMiStock::getPutinId, request.getPutinId());
        // 库存信息
        List<TMiStock> tMiStocks = tMiStockMapper.selectList(wrapper);
        List<TCkOrderdetail> tCkOrderdetails = new ArrayList<>();
        AtomicInteger i = new AtomicInteger(0);

        // Untitled-1
        // 处理后缀为1的库位
        tMiStocks.stream().forEach(tMiStock -> {
            if ("1".equals(tMiStock.getSTS())) {
                throw new RuntimeException("该库存已经占用");
            } else if ("2".equals(tMiStock.getSTS())) {
                throw new RuntimeException("该库存已经锁定");
            }
            // 获取当前日期和时间
            Calendar calendar = Calendar.getInstance();
            Date now = calendar.getTime();

            // 创建出库任务 优先创建库位后缀为1的
            String locationId = tMiStock.getLocationId();
            //判断本库位是否执行移库任务
            List<TCkPickingwavegoods> tCkPickingwavegoods = tCkPickingwavegoodsMapper.selectTypes(locationId);
            if(tCkPickingwavegoods.size()!=0 && tCkPickingwavegoods.get(0).getTypests().equals("3")){
                //判断目标库位的情况
                if(tCkPickingwavegoods.size()!=0 && !tCkPickingwavegoods.get(0).getEoutStand().isEmpty()){
                    String eoutStand = tCkPickingwavegoods.get(0).getEoutStand();
                    List<TCkPickingwavegoods> tCkPickingwavegood1 = tCkPickingwavegoodsMapper.selectTypes(eoutStand);
                    if(tCkPickingwavegood1.size()!=0 && tCkPickingwavegoods.get(0).getTypests().equals("3")){
                        throw new RuntimeException("该目的库位正在移库");
                    }
                    if(tCkPickingwavegood1.size()!=0 && tCkPickingwavegoods.get(0).getTypests().equals("2")){
                        throw new RuntimeException("该目的库位正在出库");
                    }
                    if(tCkPickingwavegood1.size()!=0 && tCkPickingwavegoods.get(0).getTypests().equals("1")){
                        throw new RuntimeException("该目的库位正在出库");
                    }
                    List<TOngoodsshelf> tOngoodsshelfs=ongoodsshelfMapper.selectAll(eoutStand);
                    if(tOngoodsshelfs.size()!=0){
                        throw new RuntimeException("该目的库位正在入库");
                    }
                }
                throw new RuntimeException("该库存正在移库");
            }
            if(tCkPickingwavegoods.size()!=0 && tCkPickingwavegoods.get(0).getTypests().equals("2")){
                throw new RuntimeException("该库存正在出库");
            }
            if(tCkPickingwavegoods.size()!=0 && tCkPickingwavegoods.get(0).getTypests().equals("1")){
                throw new RuntimeException("该库存正在出库");
            }
            List<TOngoodsshelf> tOngoodsshelf=ongoodsshelfMapper.selectAll(locationId);
            if(tOngoodsshelf.size()!=0){
                throw new RuntimeException("该库存正在入库");
            }

            TCkOrderdetail orderdetail = new TCkOrderdetail();

            TCkPickingwavegoods ckPickingwavegoods = new TCkPickingwavegoods();
            ckPickingwavegoods.setGoodId(tMiStock.getGoodsId());
            ckPickingwavegoods.setGoodsName(tMiStock.getGoodsName());
            ckPickingwavegoods.setCTL(tMiStock.getCTL());
            ckPickingwavegoods.setLocId(tMiStock.getLocationId());
            ckPickingwavegoods.setGoodsNum(tMiStock.getShelvesNum());
            ckPickingwavegoods.setGropSc(tMiStock.getGropSc());
            ckPickingwavegoods.setTypests("2");
            // ckPickingwavegoods.setWareDate(now);
            ckPickingwavegoods.setSTATUS("0");
            ckPickingwavegoods.setPICKINGID("ck" + now.getTime());

            // 库位
            TBaseStorageAreaLocation locations = locationMapper.getOne(tMiStock.getLocationId());
            // 站台
            //ckPickingwavegoods.setOUTSTAND(locations.getOutStand());
            ckPickingwavegoods.setPickingNum(tMiStock.getPackingNum());
            // plcid
            ckPickingwavegoods.setPlcId(ongoodsshelfMapper.getNextSequenceValue());
            ckPickingwavegoods.setBARCODE(tMiStock.getBARCODE());
            i.set(tCkPickingwavegoodsMapper.insertTCkPickingwavegoods(ckPickingwavegoods));
        });

        // 处理后缀为2的库位
        /*tMiStocks.stream().forEach(tMiStock -> {
            if ("1".equals(tMiStock.getSTS())) {
                throw new RuntimeException("该库存已经占用");
            } else if ("2".equals(tMiStock.getSTS())) {
                throw new RuntimeException("该库存已经锁定");
            }
            // 获取当前日期和时间
            Calendar calendar = Calendar.getInstance();
            Date now = calendar.getTime();

            // 创建出库任务
            String locationId = tMiStock.getLocationId();
            // 截取库位后缀
            *//*String suffix = locationId.substring(locationId.length() - 1);

            if (!"2".equals(suffix)) {
                return; // 只处理后缀为2的库位
            }*//*
            //判断本库位是否执行移库任务
            List<TCkPickingwavegoods> tCkPickingwavegoods = tCkPickingwavegoodsMapper.selectTypes(locationId);
            if(tCkPickingwavegoods.size()!=0 && tCkPickingwavegoods.get(0).getTypests().equals("3")){
                throw new RuntimeException("该库存正在移库");
            }
            if(tCkPickingwavegoods.size()!=0 && tCkPickingwavegoods.get(0).getTypests().equals("2")){
                throw new RuntimeException("该库存正在出库");
            }
            if(tCkPickingwavegoods.size()!=0 && tCkPickingwavegoods.get(0).getTypests().equals("1")){
                throw new RuntimeException("该库存正在出库");
            }
            List<TOngoodsshelf> tOngoodsshelf=ongoodsshelfMapper.selectAll(locationId);
            if(tOngoodsshelf.size()!=0){
                throw new RuntimeException("该库存正在入库");
            }
            //判断1层库位是否在移库
            String suffix1 = locationId.substring(0,locationId.length() - 1);
            String location1 = suffix1+"1";
            List<TCkPickingwavegoods> tCkPickingwavegoods1 = tCkPickingwavegoodsMapper.selectTypes(location1);
            if(tCkPickingwavegoods1.size()!=0 && tCkPickingwavegoods1.get(0).getTypests().equals("3")){
                throw new RuntimeException("库位:"+location1+"正在移库");
            }

            if(tCkPickingwavegoods1.size()!=0 && tCkPickingwavegoods1.get(0).getTypests().equals("3")){
                throw new RuntimeException("库位:"+location1+"正在移库");
            }
            if(tCkPickingwavegoods1.size()!=0 && tCkPickingwavegoods1.get(0).getTypests().equals("2")){
                throw new RuntimeException("库位:"+location1+"正在出库");
            }
            if(tCkPickingwavegoods1.size()!=0 && tCkPickingwavegoods1.get(0).getTypests().equals("1")){
                throw new RuntimeException("库位:"+location1+"正在出库");
            }
            List<TOngoodsshelf> tOngoodsshelf1=ongoodsshelfMapper.selectAll(location1);
            if(tOngoodsshelf1.size()!=0){
                throw new RuntimeException("库位:"+location1+"正在入库");
            }
            if(tCkPickingwavegoods.size()!=0 && tCkPickingwavegoods.get(0).getTypests().equals("3")){
                throw new RuntimeException("该库存正在移库");
            }
            TCkOrderdetail orderdetail = new TCkOrderdetail();

            TCkPickingwavegoods ckPickingwavegoods = new TCkPickingwavegoods();
            ckPickingwavegoods.setGoodId(tMiStock.getGoodsId());
            ckPickingwavegoods.setGoodsName(tMiStock.getGoodsName());
            ckPickingwavegoods.setCTL(tMiStock.getCTL());
            ckPickingwavegoods.setLocId(tMiStock.getLocationId());
            ckPickingwavegoods.setGoodsNum(tMiStock.getShelvesNum());
            ckPickingwavegoods.setGropSc(tMiStock.getGropSc());
            ckPickingwavegoods.setTypests("2");
            // ckPickingwavegoods.setWareDate(now);
            ckPickingwavegoods.setSTATUS("0");
            ckPickingwavegoods.setPICKINGID("ck" + now.getTime());

             //库位
            TBaseStorageAreaLocation locations = locationMapper.getOne(tMiStock.getLocationId());
            // 站台
            ckPickingwavegoods.setOUTSTAND(locations.getOutStand());
            ckPickingwavegoods.setPickingNum(tMiStock.getPackingNum());
            // plcid
            ckPickingwavegoods.setPlcId(ongoodsshelfMapper.getNextSequenceValue());
            ckPickingwavegoods.setBARCODE(tMiStock.getBARCODE());
            i.set(tCkPickingwavegoodsMapper.insertTCkPickingwavegoods(ckPickingwavegoods));
        });*/

        for (TMiStock tMiStock : tMiStocks) {
            tMiStock.setSTS("1");
            tMiStockMapper.updatests(tMiStock);
        }
        return i.get() > 0 ? i.get() : 0;
    }


    /**
     * 修改库位状态
     *
     * @param request
     * @return
     */
    @Override
    public int updateCkSts(UpdateStatusRequest request) {

        return tCkOrderdetailMapper.updateCkSts(request);
    }


    private String generateTimestampCode() {
        String prefix = "CK";
        long timestamp = System.currentTimeMillis(); // 获取当前时间戳
        String rfid = prefix + timestamp;
        return rfid;
    }

    @Override
    public List<TCkOrderdetail> selectTCkOrderdetailLists(TCkOrderdetail tCkOrderdetail) {
        return tCkOrderdetailMapper.selectTCkOrderdetailLists(tCkOrderdetail);
    }

}
